<template>
  <div class="app-container">
    <div>
      <el-form :inline="true">
        <el-form-item>
          <el-button type="primary" @click="addUnit(0)">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :data="unitList"
      stripe
      v-loading="dataListLoading"
      style="width: 100%"
      ref="unit"
    >
      <!-- 单元下的章节集合 -->
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-table
            :data="props.row.children"
            stripe
            style="width: 100%"
            ref="chapter"
          >
            <!-- 单元下的章节下的章节字体集合 -->
            <el-table-column type="expand">
              <template slot-scope="scope">
                <el-table :data="scope.row.children" stripe style="width: 100%">
                  <el-table-column
                    header-align="center"
                    align="center"
                    label="章节字体id"
                    prop="id"
                  >
                  </el-table-column>
                  <el-table-column
                    header-align="center"
                    align="center"
                    label="字体名称"
                    prop="name"
                  >
                  </el-table-column>
                  <el-table-column
                    header-align="center"
                    align="center"
                    label="操作"
                  >
                    <template slot-scope="scope">
                      <el-button
                        type="text"
                        @click="addWord(1, scope.row.id, scope.row)"
                        >修改</el-button
                      >
                      <el-button type="text" @click="deleteWord(scope.row.id, scope.row)"
                        >删除</el-button
                      >
                    </template>
                  </el-table-column>
                </el-table>
              </template>
            </el-table-column>
            <el-table-column
              prop="name"
              header-align="center"
              align="center"
              label="章节名称"
            ></el-table-column>
            <el-table-column
              prop="corder"
              header-align="center"
              align="center"
              label="章节顺序"
            ></el-table-column>
            <el-table-column
              prop="page"
              header-align="center"
              align="center"
              label="章节页码"
            ></el-table-column>
            <el-table-column
              prop="contentsPage"
              header-align="center"
              align="center"
              label="页号"
            ></el-table-column>
            <el-table-column header-align="center" align="center" label="操作">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  @click="addWord(0, scope.row.id, scope.row)"
                  >添加字体</el-button
                >
                <el-button
                  type="text"
                  @click="addChapter(1, scope.row.id, scope.row)"
                  >修改</el-button
                >
                <el-button type="text" @click="deleteChapter(scope.row.id, scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="单元名称"
      ></el-table-column>
      <el-table-column
        prop="uorder"
        header-align="center"
        align="center"
        label="单元顺序"
      ></el-table-column>
      <el-table-column
        prop="tip"
        header-align="center"
        align="center"
        label="重要提示"
      ></el-table-column>
      <el-table-column
        prop="contentsPage"
        header-align="center"
        align="center"
        label="页号"
      ></el-table-column>
      <el-table-column header-align="center" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="addChapter(0, scope.row.id, scope.row)"
            >添加章节</el-button
          >
          <el-button
            style=""
            type="text"
            @click="addUnit(1, scope.row.id, scope.row)"
            >修改</el-button
          >
          <el-button type="text" @click="deleteById(scope.row.id, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :visible.sync="dialogUnitVisible"
      :title="type == 0 ? '添加单元信息' : '修改单元信息'"
    >
      <el-form
        :rules="dataRule"
        ref="unitForm"
        :model="unit"
        label-width="120px"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="unit.name" />
        </el-form-item>
        <el-form-item label="顺序" prop="corder">
          <el-input-number
            v-model="unit.uorder"
            controls-position="right"
            :min="1"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="页号" prop="contentsPage">
          <el-input-number
            v-model="unit.contentsPage"
            controls-position="right"
            :min="1"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="提示" prop="tip">
          <el-input v-model="unit.tip" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogUnitVisible = false">取 消</el-button>
        <el-button
          :disabled="saveUnitBtnDisabled"
          type="primary"
          @click="saveOrUpdate"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="dialogChapterVisible"
      :title="type == 0 ? '添加章节信息' : '修改章节信息'"
    >
      <el-form
        :rules="dataRule1"
        ref="chapterForm"
        :model="chapter"
        label-width="120px"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="chapter.name" />
        </el-form-item>
        <el-form-item label="顺序" prop="corder">
          <el-input-number
            v-model="chapter.corder"
            controls-position="right"
            :min="1"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="页码" prop="page">
          <el-input-number
            v-model="chapter.page"
            controls-position="right"
            :min="1"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="页号" prop="contentsPage">
          <el-input-number
            v-model="chapter.contentsPage"
            controls-position="right"
            :min="1"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogChapterVisible = false">取 消</el-button>
        <el-button
          :disabled="saveChapterBtnDisabled"
          type="primary"
          @click="saveOrUpdate1"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="dialogWordVisible"
      :title="type == 0 ? '添加章节字体信息' : '修改章节字体信息'"
    >
      <el-form
        :rules="dataRule2"
        ref="wordForm"
        :model="word"
        label-width="120px"
      >
        <el-form-item label="顺序" prop="corder">
          <el-input-number
            v-model="word.cporder"
            controls-position="right"
            :min="1"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="字体" prop="tid">
          <el-select v-model="word.tid" filterable placeholder="请选择字体">
            <el-option
              v-for="z in zList"
              :key="z.id"
              :label="z.name"
              :value="z.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogWordVisible = false">取 消</el-button>
        <el-button
          :disabled="saveWordBtnDisabled"
          type="primary"
          @click="saveOrUpdate2"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <div>
      <!-- <el-button @click="previous">上一步</el-button> -->
      <!-- <el-button :disabled="saveBtnDisabled" type="primary" @click="next"
        >下一步</el-button
      > -->
    </div>
  </div>
</template>

<script>
import Vue from "vue";
export default {
  data() {
    return {
      hang: {},
      expands: ["1427143603920244737"],
      saveBtnDisabled: false, // 下一步按钮是否禁用
      bookId: null,
      unit: {
        // 单元对象
        name: "",
        uorder: 1,
        id: null,
        tip: null,
        bid: null,
        contentsPage: 1,
      },
      chapter: {
        id: null,
        name: "",
        page: 1,
        uid: null,
        corder: 1,
        contentsPage: 1,
      }, // 单元章节
      word: {
        // 章节字体
        id: null,
        cid: null,
        tid: null,
        cporder: 1,
      },
      unitList: [], // 单元集合
      dataListLoading: false, // 加载单元数据
      type: 0, // 0:添加1:修改
      dialogUnitVisible: false, // 添加单元信息对话框
      dialogChapterVisible: false, // 添加单元章节对话框
      dialogWordVisible: false, // 添加章节字体对话框
      saveUnitBtnDisabled: false, // 单元信息保存按钮是否禁用
      saveChapterBtnDisabled: false, // 单元章节保存按钮是否禁用
      saveWordBtnDisabled: false, // 单元章节保存按钮是否禁用
      dataRule: {
        // 数据规则
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        tip: [{ required: true, message: "提示不能为空", trigger: "blur" }],
      },
      dataRule1: {
        // 数据规则
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        // page: [{ required: true, message: "页码不能为空" }],
      },
      dataRule2: {
        // 数据规则
        tid: [{ required: true, message: "字体不能为空", trigger: "blur" }],
      },
      zList: [], // 所有字
    };
  },

  activated() {
    // 看看是哪里来的
    this.dataListLoading = true;
    this.bookId = this.$route.params.id;
    this.getUnitByBookId();
    this.$http({
      url: this.$http.adornUrl(`/sf/cbtypeface/getWordAllList`),
      method: "get",
    }).then(({ data }) => {
      if (data && data.code === 0) {
        this.zList = data.zList;
      }
    });
  },

  methods: {
    rowKey(row) {
      return row.id;
    },
    // 获取固定单元数据
    getUnitByBookId() {
      let height = this.$refs.unit.bodyWrapper.scrollHeight;
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl(`/sf/cbfixunit/list`),
        method: "get",
        params: this.$http.adornParams({
          bid: this.bookId,
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          console.log(data)
          this.unitList = data.unitList;
        } else {
          this.unitList = [];
        }
        for (let i = 0; i < this.unitList.length; i++) {
          if (this.hang.id == this.unitList[i].id || this.hang.uid == this.unitList[i].id) {
            this.$nextTick(() => {
              this.$refs.unit.toggleRowExpansion(this.unitList[i], true);
              if(this.hang.uid == this.unitList[i].id){
                for(let j = 0; j < this.unitList[i].children.length; j++){
                  if(this.hang.id == this.unitList[i].children[j].id){
                    this.$nextTick(() => {
                      this.$refs.chapter.toggleRowExpansion(this.unitList[i].children[j], true);
                    })
                  }
                }
              }
            }); 
            break;
          }
          for(let j = 0; j < this.unitList[i].children.length; j++){
            if(this.hang.cid == this.unitList[i].children[j].id){
                this.$nextTick(() => {
                  this.$refs.unit.toggleRowExpansion(this.unitList[i], true);
                  this.$nextTick(() => {
                    this.$refs.chapter.toggleRowExpansion(this.unitList[i].children[j], true);
                  })
                })
                break;
            }
          }
        }
        this.dataListLoading = false;
      });
    },
    // 单元对话框中的保存或修改
    saveOrUpdate() {
      if (this.type == 0) {
        // 添加单元
        this.unitFormSubmit();
      } else {
        // 修改单元
        this.updateUnit();
      }
    },
    // 单元章节对话框中的保存或修改
    saveOrUpdate1() {
      if (this.type == 0) {
        // 添加章节
        this.chapterFormSubmit();
      } else {
        // 修改章节
        this.updateChapter();
      }
    },
    // 字体对话框中的保存或修改
    saveOrUpdate2() {
      if (this.type == 0) {
        // 添加章节字体
        this.wordFormSubmit();
      } else {
        // 修改字体
        this.updateWord();
      }
    },
    // 删除单元章节
    deleteChapter(id, row) {
      this.$confirm(`确定对[章节id=${id}]进行[删除]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.hang = row;
          this.$http({
            url: this.$http.adornUrl("/sf/cbfixchapter/delete"),
            method: "post",
            data: this.$http.adornData([id], false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "删除成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getUnitByBookId();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
    // 删除字体
    deleteWord(id, row) {
      this.$confirm(`确定对[章节字体id=${id}]进行[删除]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.hang = row;
          this.$http({
            url: this.$http.adornUrl("/sf/cbfixchaptertypeface/delete"),
            method: "post",
            data: this.$http.adornData([id], false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "删除成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getUnitByBookId();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
    // 更新单元章节信息
    updateChapter() {
      this.$refs["chapterForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbfixchapter/update`),
            method: "post",
            data: this.$http.adornData(
              {
                id: this.chapter.id,
                name: this.chapter.name,
                page: this.chapter.page,
                corder: this.chapter.corder,
                contentsPage: this.chapter.contentsPage,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "修改成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getUnitByBookId();
                  this.dialogChapterVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 打开对话框，添加、修改
    addUnit(status, id, row) {
      this.hang = row;
      // 重置表单 应该只是把表单有的清空而已
      this.$nextTick(() => {
        this.$refs.unitForm.resetFields(); // this.$refs['unitForm'].resetFields(); 可能不识别 、还是不识别
      });
      if (status == 0) {
        // 添加
        this.type = 0;
        // 清空
        this.unit.id = null;
      } else {
        // 修改
        this.type = 1;
        // 回显数据
        this.$http({
          url: this.$http.adornUrl("/sf/cbfixunit/info/" + id),
          method: "get",
        }).then(({ data }) => {
          this.unit.id = data.cbFixUnit.id;
          this.unit.name = data.cbFixUnit.name;
          this.unit.uorder = data.cbFixUnit.uorder;
          this.unit.tip = data.cbFixUnit.tip;
          this.unit.bid = data.cbFixUnit.bid;
          this.unit.contentsPage = data.cbFixUnit.contentsPage;
        });
      }
      this.dialogUnitVisible = true;
    },
    addChapter(status, id, row) {
      this.hang = row;
      // 重置表单 应该只是把表单有的清空而已
      this.$nextTick(() => {
        this.$refs.chapterForm.resetFields(); // this.$refs['chapterForm'].resetFields(); 可能不识别 、还是不识别
      });
      if (status == 0) {
        // 添加
        this.type = 0;
        // 清空
        this.chapter.id = null;
        this.chapter.uid = id;
      } else {
        // 修改
        this.type = 1;
        // 回显数据
        this.$http({
          url: this.$http.adornUrl("/sf/cbfixchapter/info/" + id),
          method: "get",
        }).then(({ data }) => {
          this.chapter.id = data.cbFixChapter.id;
          this.chapter.name = data.cbFixChapter.name;
          this.chapter.corder = data.cbFixChapter.corder;
          this.chapter.page = data.cbFixChapter.page;
          this.chapter.uid = data.cbFixChapter.uid;
          this.chapter.contentsPage = data.cbFixChapter.contentsPage;
        });
      }
      this.dialogChapterVisible = true;
    },
    // 打开字体对话框
    addWord(status, id, row) {
      this.hang = row;
      // 重置表单 应该只是把表单有的清空而已
      this.$nextTick(() => {
        this.$refs.wordForm.resetFields();
      });
      if (status == 0) {
        // 添加
        // 清空
        this.word.cid = id;
        this.word.id = null;
        this.word.tid = null;
        this.dialogWordVisible = true;
      } else {
        // 修改
        // 回显数据
        this.$http({
          url: this.$http.adornUrl("/sf/cbfixchaptertypeface/info/" + id),
          method: "get",
        }).then(({ data }) => {
          this.word.id = data.cbFixChapterTypeface.id;
          this.word.tid = data.cbFixChapterTypeface.tid;
          this.word.cporder = data.cbFixChapterTypeface.cporder;
          this.word.cid = data.cbFixChapterTypeface.cid;
          this.dialogWordVisible = true;
        });
      }
      this.type = status;
    },
    // 通过单元id删除单元
    deleteById(id, row) {
      this.$confirm(`确定对[单元id=${id}]进行[删除]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.hang = row;
          this.$http({
            url: this.$http.adornUrl("/sf/cbfixunit/delete"),
            method: "post",
            data: this.$http.adornData([id], false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "删除成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getUnitByBookId();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
    // 表单提交
    unitFormSubmit() {
      // 表单验证
      this.$refs["unitForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbfixunit/save`),
            method: "post",
            data: this.$http.adornData(
              {
                bid: this.bookId,
                name: this.unit.name,
                uorder: this.unit.uorder,
                tip: this.unit.tip,
                contentsPage: this.unit.contentsPage,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "添加成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.hang = {"id": data.unit.id};
                  this.getUnitByBookId();
                  this.dialogUnitVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 字体表单提交
    wordFormSubmit() {
      // 表单验证
      this.$refs["wordForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbfixchaptertypeface/save`),
            method: "post",
            data: this.$http.adornData(
              {
                cporder: this.word.cporder,
                cid: this.word.cid,
                tid: this.word.tid,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "添加成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getUnitByBookId();
                  this.dialogWordVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 章节表单提交
    chapterFormSubmit() {
      // 表单验证
      this.$refs["chapterForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbfixchapter/save`),
            method: "post",
            data: this.$http.adornData(
              {
                uid: this.chapter.uid,
                name: this.chapter.name,
                page: this.chapter.page,
                corder: this.chapter.corder,
                contentsPage: this.chapter.contentsPage,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "添加成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getUnitByBookId();
                  this.dialogChapterVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    updateUnit() {
      // 表单验证,只需要验证单元名称
      this.$refs["unitForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbfixunit/update`),
            method: "post",
            data: this.$http.adornData(
              {
                id: this.unit.id,
                name: this.unit.name,
                uorder: this.unit.uorder,
                tip: this.unit.tip,
                contentsPage: this.unit.contentsPage,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "修改成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getUnitByBookId();
                  // this.$refs.unit.toggleRowExpansion(this.hang, true);
                  this.dialogUnitVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    updateWord() {
      // 表单验证,只需要验证单元名称
      this.$refs["wordForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbfixchaptertypeface/update`),
            method: "post",
            data: this.$http.adornData(
              {
                id: this.word.id,
                tid: this.word.tid,
                cporder: this.word.cporder,
              },
              false
            ),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "修改成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getUnitByBookId();
                  this.dialogWordVisible = false;
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.chanpterList {
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}
.chanpterList li {
  position: relative;
}
.chanpterList p {
  float: left;
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 70px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #ddd;
}
.chanpterList .acts {
  float: right;
  font-size: 14px;
}

.videoList {
  padding-left: 50px;
}
.videoList p {
  float: left;
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 50px;
  line-height: 30px;
  width: 100%;
  border: 1px dotted #ddd;
}
</style>
